let app = getApp()

Page({
	data: {
		registerButtonStatus: true,
		userInfoDict: {
			avatartUrl: "https://pic1.zhimg.com/80/v2-6ac760cd2a2f63b910aa6417bad9e117_1440w.jpg",
			nickName: "",
		},
		inputLists: [
			{
				name: "nickName",
				inputText: "昵称",
				maxLength: 11,
				type: "text",
				value: "",
			},
      {
				name: "studentName",
				inputText: "姓名",
				maxLength: 4,
				type: "text"
			},
      {
				name: "classInfo",
				inputText: "学院班级",
				maxLength: 20,
				type: "text"
			},
		],

		items: [
      {
				checked: true, 
				disabled: false, 
				value: 'male', 
				desc: '男', 
				id: 'checkbox1' 
			},
      { 
				checked: false, 
				disabled: false, 
				value: 'female', 
				desc: '女', 
				id: 'checkbox2' 
			},
    ],

    buttonText: "完成注册",
	},

	onLoad(options) {
		this.setData({
			phoneNumber: options.phoneNumber 
		})

		if (app.globalData.userInfo) {
			let {avatar,nick,nickName} = app.globalData.userInfo
			let userInfoDict = this.data.userInfoDict
			
			userInfoDict.avatartUrl = avatar
			userInfoDict.nickName = nick || nickName
			
			var inputNickname = 'inputLists[' + 0 + '].value';

			this.setData({
				registerButtonStatus: false,
				userInfoDict,
				[inputNickname]: nickName,
			})
		}
	},
  
	radioChange(e) {
    console.log('你选择的是：', e.detail.value);
  },

	onPreviewAvatarImage() {
		my.previewImage({
			current: 0,
			urls: [
				this.data.userInfoDict.avatartUrl
			],
		})
	},

	onGetAuthorize(res) {
		my.getOpenUserInfo({
			fail: (res) => {},
			success: (res) => {
				let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
				//有选择的获取用户数据
				let {
					avatar,
					city,
					gender,
					nickName,
					province
				} = userInfo

				//更新到视图中
				let userInfoDict = this.data.userInfoDict
				userInfoDict.avatartUrl = avatar
				userInfoDict.nickName = nickName
				
				//获取昵称放入输入框中
				var inputNickname = 'inputLists[' + 0 + '].value';

				this.setData({
					userInfoDict,
					registerButtonStatus: false,
					[inputNickname]: nickName,
				})
				app.globalData.userInfo = userInfoDict
			},
		});
	},

	onSubmit(e) {
		console.log(e)
		my.showLoading({
			content: '加载中...',
			delay: 300,
		})

		const url = app.globalData.baseUrl + app.globalData.registerUrl

		const {
			studentName,
			classInfo,
			gender,
			nickName
		} = e.detail.value

		let promise = new Promise((resolve, reject) => {
			my.request({
				url,
				data: {
					phoneNumber: this.data.phoneNumber,
					studentName,
					classInfo,
					gender,
					avatar: this.data.userInfoDict.avatartUrl,
					nickName
				},
				headers: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				method: "POST",
				success: (res) => {
					resolve(res)
				}
			})
		})

		promise.then((res) => {
			console.log(res)
			my.hideLoading()
			if (res.data.status == "fail") {			
				my.showToast({
					type: 'fail',
					content: res.data.message,
					duration: 2000,
				})
			} else {
				//保存cookie
				console.log(res)
				const cookie = cookieUtil.getSessionIdFromResponse(res)
				console.log(cookie)
				cookieUtil.setCookieToStorage(cookie)
				
				//保存用户名
				//app.globalData.userInfo.name = studentName
				my.showToast({
					type: 'success',
					content: res.data.message,
					duration: 2000,
					success: () => {
						app.globalData.userStatus = true
						my.switchTab({
							url: '/pages/my_new/my_new'
						})
					}
				})
			}
		})
	},
});
